﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using ET;
using System.Data.SqlClient;
namespace DA
{
    public class Order_DAL : clsConnect
    {
        private clsConnect cls = new clsConnect();
        #region[Get-Data]
        public DataTable GetOrder()
        {
            return cls.GetData("sp_tb_Order_Search");
        }
        public DataTable GetOrderOrder()
        {
            return cls.GetData("sp_tb_Order_Buy_Order");
        }
        public DataTable GetOrderDetail_Edit(int id)
        {
            return cls.GetData_ID_Int("sp_tb_Order_Edit", "@Order_Id", id);
        }
        #endregion

        #region[Insert-Update-Delete]
        public bool Inserttb_Order(Order_ET data)
        {

            SqlCommand cmd = new SqlCommand();
            cmd.Connection = GetConnection();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "sp_tb_Order_Insert";

            cmd.Parameters.Add("@Order_Code", SqlDbType.NVarChar).Value = data.Order_Code;
            cmd.Parameters.Add("@Order_Date", SqlDbType.DateTime).Value = data.Order_Date;
            cmd.Parameters.Add("@Customer_Id", SqlDbType.Int).Value = data.Customer_Id;
            cmd.Parameters.Add("@Order_DateBegin", SqlDbType.DateTime).Value = data.Order_DateBegin;
            cmd.Parameters.Add("@Order_DateEnd", SqlDbType.DateTime).Value = data.Order_DateEnd;
            cmd.Parameters.Add("@Order_Payment_Date", SqlDbType.DateTime).Value = data.Order_Payment_Date;
            cmd.Parameters.Add("@Order_Transport", SqlDbType.Float).Value = data.Order_Transport;
            cmd.Parameters.Add("@Order_Note", SqlDbType.NVarChar).Value = data.Order_Note;

            cmd.Parameters.Add("@Order_VAT", SqlDbType.Float).Value = data.Order_VAT;
            cmd.Parameters.Add("@Order_Total", SqlDbType.Float).Value = data.Order_Total;
            cmd.Parameters.Add("@Order_Total_Payment", SqlDbType.Float).Value = data.Order_Total_Payment;
            cmd.Parameters.Add("@IsActive", SqlDbType.Bit).Value = data.IsActive;
            cmd.Parameters.Add("@Order_Id", SqlDbType.Int).Direction = ParameterDirection.Output;
            cmd.ExecuteNonQuery();
            data.Order_Id = int.Parse(cmd.Parameters["@Order_Id"].Value.ToString());
            return true;
        }
        public bool Updatetb_Order(Order_ET data)
        {
            cls.CreateNewSqlCommand();
            cls.AddParameter("@Order_Id", data.Order_Id);
            cls.AddParameter("@Order_Code", data.Order_Code);
            cls.AddParameter("@Order_Date", data.Order_Date);
            cls.AddParameter("@Customer_Id", data.Customer_Id);
            cls.AddParameter("@Order_DateBegin", data.Order_DateBegin);
            cls.AddParameter("@Order_DateEnd", data.Order_DateEnd);
            cls.AddParameter("@Order_Payment_Date", data.Order_Payment_Date);
            cls.AddParameter("@Order_Transport", data.Order_Transport);
            cls.AddParameter("@Order_Note", data.Order_Note);
            cls.AddParameter("@Order_VAT", data.Order_VAT);
            cls.AddParameter("@Order_Total", data.Order_Total);
            cls.AddParameter("@Order_Total_Payment", data.Order_Total_Payment);
            cls.ExecuteNonQuery("sp_tb_Order_Update");
            return true;
        }
        public bool Delete_Order(int id)
        {
            cls.Delete("sp_tb_Order_Delete", "@Order_Id", id);
            return true;
        }
        #endregion

        #region[Search-Fillter]
        public DataTable SearchOrder(string keywords)
        {
            return cls.Search("sp_tb_Order_Search", "@KEYWORDS", keywords);
        }
        #endregion
    }
}
